package com.iotimc.attendancesystem.io;

import android.util.Log;

import com.iotimc.attendancesystem.App;
import com.iotimc.attendancesystem.entity.BasicSettingEntity;
import com.iotimc.comass.bean.IoInCommon;
import com.iotimc.comass.bean.IoOutCommon;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * @author david
 * @date 2017/12/1
 */

public class IoCommonPresenter {
    private static final String TAG = "IoCommonPresenter";
    private Disposable timeDisposable;
    /**
     * 实际门的状态
     */
    private IoInCommon ioInCommon;
    /**
     * 锁的状态(开了锁不代表门已经开了。开门需要时间)
     */
    private IoOutCommon ioOutCommon;

    private static class SingletonHolder {
        private static final IoCommonPresenter INSTANCE = new IoCommonPresenter();
    }

    public static IoCommonPresenter getInstance() {
        return IoCommonPresenter.SingletonHolder.INSTANCE;
    }

    private IoCommonPresenter() {
        Log.i(TAG, "IoCommonPresenter: ");



        //门的状态
        ioInCommon = new IoInCommon(new IoInCommon.OnListener() {
            /**
             * @param result 操作结果，0为成功，1为失败
             * @param addr 地址，十六进制 0xFFFE
             * @param type 保留字段
             */
            @Override
            public void opened(short result, short addr, int type) {
                Log.i(TAG, " 门的状态 opened: "+result);

            }

            /**
             * @param result 操作结果，0为成功，1为失败
             * @param addr 地址，十六进制 0xFFFE
             * @param type 保留字段
             */
            @Override
            public void closed(short result, short addr, int type) {
                Log.i(TAG, " 门的状态 closed: "+result);

            }

            /**
             * @param result 操作结果，0为成功，1为失败
             * @param addr 地址，十六进制  0xFFFE
             * @param status IO状态，1为open状态，2为close状态
             */
            @Override
            public void getStatus(short result, short addr, short status) {

            }
        });
        //锁状态
        ioOutCommon = new IoOutCommon(new IoOutCommon.OnListener() {
            /*
            result:操作结果，0为成功，1为失败
            addr:地址，十六进制 0xFFFE
            type:保留字段
            */
            @Override
            public void opened(short result, short addr, int type) {
                Log.i(TAG, "opened: 开锁result："+result);
                BasicSettingEntity entity = App.getInstance().getBasicSettingEntity();
                if (entity.getDelayDoorSecond() <= 0) {
                    return;
                } else if (timeDisposable == null) {
                    timeClose(entity);
                } else {
                    if (!timeDisposable.isDisposed()) {
                        timeDisposable.dispose();
                    }
                    timeClose(entity);
                }
            }

            /*
            result:操作结果，0为成功，1为失败
            addr:地址，十六进制  0xFFFE
            type:保留字段
            */
            @Override
            public void closed(short result, short addr, int type) {
                Log.i(TAG, "opened: 关锁："+result);
                if (timeDisposable != null && !timeDisposable.isDisposed()) {
                    timeDisposable.dispose();
                }
            }

            /**
             * @param result 操作结果，0为成功，1为失败
             * @param addr 十六进制 0xFFFE
             * @param status IO状态，1为open状态，2为close状态
             */
            @Override
            public void getStatus(short result, short addr, short status) {
                Log.i(TAG, "getStatus: status"+status);
                   /* if (status == 2){
                        close();
                    }else {
                        open();
                    }*/

            }
        });

    }

    public void open() {
        ioOutCommon.open();
    }

    public void close() {
        ioOutCommon.close();
    }

    public void getStatus(){
        ioOutCommon.getStatus();
    }

    private void timeClose(BasicSettingEntity entity) {
        timeDisposable = Observable.timer(entity.getDelayDoorSecond(), TimeUnit.SECONDS).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                ioOutCommon.close();
            }
        });
    }
}
